package com.asman.vr

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager

/**
 *    author : ybk
 *    date   : 2019-07-11 20:42
 *    desc   : 正在加载的dialog
 *    progress,title
 */
class DialogLoadingFragment : AppCompatDialogFragment() {

    companion object {
        private const val TITLE = "title"

        fun newInstance(title: String) = DialogLoadingFragment().apply {
            arguments = Bundle().apply {
                putString(TITLE, title)
            }
        }
    }

    private var title: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            title = it.getString(TITLE)
        }
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog?.window?.setBackgroundDrawableResource(android.R.color.transparent) // 一句话搞定
        isCancelable = false
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        dialog?.setOnKeyListener { dialog, keyCode, event ->
            when (keyCode) {
                KeyEvent.KEYCODE_BACK -> {
                    dismiss()
                    true
                }
                else -> true
            }
        }
        
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogFullScreen) //dialog全屏
        var view = inflater.inflate(R.layout.fragment_dialog_loading, container, false)
        tv_title = view.findViewById(R.id.tv_title)
        return view
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        tv_title?.text = title
    }

    fun show(manager: FragmentManager) {
        show(manager, "DialogLoadingFragment")
    }

    fun setMsg(msg: String) {
        tv_title?.text = msg
    }

    // todo 自己加的不知道对不对
    var tv_title: TextView? = null
}
